package com.hengfoxwolf.commonbase.base.util
import android.app.Activity
import android.content.res.Resources

/*
* 1.尽量只在当前页面生效，包括activity，fragment，dialog，view，需要在setCOntentView()或者inflate之前调用这个方法，
*   在结束onDestroy,onDismiss,onDetachWindow()的时候调用resetScreen()方法。
* 2.在页面中需要弹出toast和dialog的时候需要调用resetScreen，不然toast和dialog的页面大小和字体大小会被影响。
* 3.记住一点使用前调用adapterScreen，时候后调用resetScreen
* */
object ScreenUtil {
    fun adapterScreen(activity: Activity, targetDp: Float, vertical: Boolean = true) {
        //系统屏幕尺寸
        val systemDM = Resources.getSystem().displayMetrics
        //应用屏幕尺寸
        val appDM = activity.application.resources.displayMetrics
        //activity屏幕尺寸
        val activityDM = activity.resources.displayMetrics
        if (vertical) {
            //适配屏幕高度
            activityDM.density = activityDM.widthPixels / targetDp
        } else {
            //适配屏幕宽度
            activityDM.density = activityDM.heightPixels / targetDp
        }
        //适配相应比例的字体
        activityDM.scaledDensity = activityDM.density * (systemDM.scaledDensity / systemDM.density)
        //适配dpi
        activityDM.densityDpi = (160 * activityDM.density).toInt()
    }

    fun resetScreen(activity: Activity) {
        //系统屏幕尺寸
        val systemDM = Resources.getSystem().displayMetrics
        //应用屏幕尺寸
        val appDM = activity.application.resources.displayMetrics
        //activity屏幕尺寸
        val activityDM = activity.resources.displayMetrics

        activityDM.density = systemDM.density
        activityDM.scaledDensity = systemDM.scaledDensity
        activityDM.densityDpi = systemDM.densityDpi

        appDM.density = systemDM.density
        appDM.scaledDensity = systemDM.scaledDensity
        appDM.densityDpi = systemDM.densityDpi
    }
}